/**
 * User: yuhao
 * Date: 14-9-15
 * Time: 上午10:46
 * 地图窗口
 */

define(
    [
        "require",
        "dojo/_base/declare",
        "dojo/_base/lang",
        "dojo/_base/array",
        "dojo/dom-attr",
        "dojo/dom-class",
        "dojo/dom-construct",
        "dojo/dom-style",
        "dojo/on",
        "dojo/query",
        "dojo/topic",
        "dojo/string",
        "esri/layers/GraphicsLayer",
        "esri/renderers/SimpleRenderer",
        "esri/graphic",
        "esri/geometry/Polygon",
        "esri/graphicsUtils",
        "../appConfig",
        "../../gistech/widgets/Map/Map"
    ],
    function (require, declare, lang, array, domAttr, domClass, domConstruct, domStyle, on, query, topic, string, GraphicsLayer, SimpleRenderer, Graphic, Polygon, graphicsUtils, appConfig, Map) {

        return declare([Map], {

            constructor:function (args) {
                
            },

            postCreate:function () {
                this.init();
                this.addEvents();
                this.inherited(arguments);
            },

            init:function () {
            	 on(window, "resize", function () {//使地图地图随浏览器窗口大小改变
                    //console.log("屏幕大小改变了");
                    dojo.byId("wrapper").style.width="100%";
                    dojo.byId("wrapper").style.height="100%";
                 });
                this.inherited(arguments);
            },

            addEvents:function () {
                this.inherited(arguments);
            },

            /**
             * override
             * @param onload
             * @param map
             */
            onMapLoaded:function (onload, args) {
                this.inherited(arguments);
                var map = this.map;

                this.connects.push(on(map, "resize", lang.hitch(this, function () {
                    this.resizeMap();
                })));

                this.connects.push(on(map, "extent-change", lang.hitch(this, function (args) {
                    topic.publish("mapExtentChanged", args);
                })));
                
                topic.publish("mapLoaded", {"map":map});
            },

            /**
             * 加载方案中的可见图层
             * @param args
             */
            loadLayers:function () {
                var layerConfigs = this.planManager.getLayerConfigs();
                //遍历加载可见图层
                array.forEach(layerConfigs, function (layerConfig) {
                    var visible = layerConfig.cfg.visible;
                    if (visible) {
                        var layerConfigId = layerConfig.id;
                        this.loadLayer(layerConfigId, false);
                    }
                }, this);
            },

            /**
             * 删除图层配置的响应
             * @param args
             */
            handleLayerConfigDeleted:function (args) {
                var layerConfigIds = args.ids;
                //移除map上的图层
                array.forEach(layerConfigIds, function (layerConfigId) {
                    var layer = this.map.getLayer(layerConfigId);
                    if (layer)
                        this.map.removeLayer(layer);
                }, this);
            },

            /**
             * 图层可见性变化的响应
             * @param args
             */
            handleLayerVisibilityUpdated:function (args) {
                var layerConfigId = args.id,
                    visible = args.visible,
                    layer = this.map.getLayer(layerConfigId);

                //可见
                if (visible) {
                    //图层已加载，显示
                    if (layer) {
                        layer.show();
                    }
                    //图层未加载，创建
                    else {
                        this.loadLayer(layerConfigId, true);
                    }
                }
                //不可见
                else {
                    //图层已加载，隐藏
                    if (layer)
                        layer.hide();
                }
            },

            /**
             * 添加图层到地图的响应
             * @param args
             */
            handleAddLayerToMap:function (args) {
                var layerConfigId = args.layerConfigId,
                    zoom = args.zoom;
                this.loadLayer(layerConfigId, zoom);
            },

            /**
             * 加载图层到地图
             * @param layerConfigId
             * @param zoom
             */
            loadLayer:function (layerConfigId, zoom) {
                var layerConfig = this.planManager.getLayerConfig(layerConfigId),
                    dataSourceId = layerConfig.dataSourceId,
                    dataConfig = this.dataManager.getDataConfig(dataSourceId);

                //加载可见图层
                this.dataManager.getDataContent(dataSourceId, lang.hitch(this, function (dataContent) {
                    //根据类型创建图层
                    var layer = null,
                        source = dataConfig.source;

                    //oss数据源的
                    if (source === appConfig.dics.dataSource.oss) {
                        var layerType = dataConfig.cfg.type.name;
                        if (layerType === appConfig.dics.layerType.HEATMAP) {
                        }
                        else if (layerType === appConfig.dics.layerType.CLUSTER) {
                        }
                        else if (layerType === appConfig.dics.layerType.CHART) {
                        }
                        else {
                            layer = layerUtil.createGraphicsLayerFromOss(this.map, dataConfig, layerConfig, dataContent);
                        }
                    }
                    //eds数据源的
                    else if (source === appConfig.dics.dataSource.eds) {
                        //TODO:创建点图层或区划图层
                    }

                    if (layer)
                        this.map.addLayer(layer);
                    if (zoom) {
                        //TODO:需提炼缩放到不同类型图层的方法
                    }
                }));
            },

            /**
             * 重定位map
             */
            resizeMap:function () {
                if (this.map) {
                    this.map.resize();
                    this.map.reposition();
                }
            }
        });
    });